/*
 * @Author: 
 * @Date: 2024-11-28 16:35:39
 * @LastEditor: zhuxiaoyi
 * @LastEditTime: 2024-12-03 17:31:21
 * @Description: 用户控制器
 */
import {
  Controller,
  Get,
  Post,
  Put,
  Del,
  Param,
  Body,
  Query,
  Inject,
} from '@midwayjs/core';
import { UserService } from '../service/user.service';
import { CreateUserDto, UpdateUserDto, LoginDto } from '../dto/User.dto';

@Controller('/user')
export class UserController {
  @Inject()
  private userService: UserService;

  /**
   * 用户注册
   */
  @Post('/register')
  async register(@Body() body: CreateUserDto) {
    try {
      const { username, email, password,roleId,subsystemId} = body;
      const result = await this.userService.register(username, email, password,roleId,subsystemId);
      return {
        success: result.success,
        data: result.data || null,
        message: result.message,
      };
    } catch (error) {
      console.error('注册失败:', error.message);
      return { success: false, message: '注册失败，请稍后再试' };
    }
  }

  /**
   * 用户登录
   */
  @Post('/login')
  async login(@Body() body: LoginDto) {
    try {
      const { username, password } = body;
      const result = await this.userService.login(username, password);
      return {
        success: result.success,
        data: result.data || null,
        message: result.message,
      };
    } catch (error) {
      console.error('登录失败:', error.message);
      return { success: false, message: '登录失败，请稍后再试' };
    }
  }

  /**
   * 获取所有用户（支持分页）
   */
  @Get('/')
  async getAllUsers(@Query('page') page = 1, @Query('limit') limit = 10, @Query('search') search = '') {
    try {
      const result = await this.userService.getAllUsers(page, limit, search);
      return {
        success: true,
        data: result.data,
        total: result.total,
        message: result.message,
      };
    } catch (error) {
      console.error('获取用户列表失败:', error.message);
      return { success: false, message: '获取用户列表失败，请稍后再试' };
    }
  }

  /**
   * 根据 ID 获取用户
   */
  @Get('/:id')
  async getUserById(@Param('id') id: number) {
    try {
      const result = await this.userService.getUserById(id);
      return {
        success: result.success,
        data: result.success ? result.data : null,
        message: result.message,
      };
    } catch (error) {
      console.error('获取用户详情失败:', error.message);
      return { success: false, message: '获取用户详情失败，请稍后再试' };
    }
  }

  /**
   * 更新用户信息
   */
  @Put('/:id')
  async updateUser(@Param('id') id: number, @Body() body: UpdateUserDto) {
    try {
      const result = await this.userService.updateUser(id, body);
      return {
        success: result.success,
        data: result.success ? result.data : null,
        message: result.message,
      };
    } catch (error) {
      console.error('更新用户信息失败:', error.message);
      return { success: false, message: '更新用户信息失败，请稍后再试' };
    }
  }

  /**
   * 删除用户
   */
  @Del('/:id')
  async deleteUser(@Param('id') id: number) {
    try {
      const result = await this.userService.deleteUser(id);
      return {
        success: result.success,
        message: result.message,
      };
    } catch (error) {
      console.error('删除用户失败:', error.message);
      return { success: false, message: '删除用户失败，请稍后再试' };
    }
  }
}
